GBML manual / Instances

instance_max

The most instances of `obj` that can ever exist at the same time. Fixed when the game is built - it never changes while the game is running.

Syntax

instance_max(obj) -> int

Arguments

NameType
objobject

Returns

int

Description

Tells you the most instances of an object that can ever exist at the same time. This number is fixed when the game is built - it never changes while the game is running.

Every object has a limit like this. instance_create uses it too: once an object is already at its limit, instance_create stops making new ones. It does this with no error and no crash. It just hands back nothing instead of a new instance.

That failure with no message is the trap. Say your code spends gold, then creates a tower. If the create fails, the gold is still spent - but no tower appears. instance_max is how you check the limit yourself, before you spend anything, instead of finding out the hard way.

Example

if (instance_count(obj_doc_enemy) < instance_max(obj_doc_enemy)) {
    instance_create(x, y, obj_doc_enemy);
}

Only spawns another obj_doc_enemy while there is still room for one. instance_count counts how many exist right now; instance_max says how many can ever exist. Checking both, before creating anything, is what keeps a spawner from wasting whatever it was about to spend on a create that would have done nothing.

This check is exact for an object that has no children. If the object is a parent, instance_count also counts every child, and each child has a limit of its own, so the count can be bigger than the parent's own instance_max. The check then says "full" a little too soon, never too late.

On the GBA

This is exactly the ceiling instance_create(...) enforces by silently returning `nothing` instead of a new instance - a full pool is normal, not a crash, so nothing else warns you when you cross it. The habit this pairs with: check `instance_count(obj) < instance_max(obj)` BEFORE calling instance_create(obj, ...), not after - especially whenever a create is the reward for spending something (gold, an item, a cooldown) that a silently-failed create would otherwise spend for nothing. Named `instance_max` rather than `max_instances` on purpose, to sit next to instance_count - every other pair in this table already puts the shared noun first (room_width/room_height, instance_count itself) rather than borrowing another tool’s own word order.

See also: instance_countinstance_create